package com.onlineshop.controller.admin;

import com.onlineshop.dao.OrderDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/admin/orders/update")
public class UpdateOrderStatusServlet extends HttpServlet {
    private OrderDao orderDao = new OrderDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderIdStr = request.getParameter("orderId");
        String status = request.getParameter("status");

        if (orderIdStr == null || orderIdStr.isEmpty() || status == null || status.isEmpty()) {
            response.sendRedirect(request.getContextPath() + "/admin/orders?error=参数错误");
            return;
        }

        try {
            int orderId = Integer.parseInt(orderIdStr);
            boolean success = orderDao.updateOrderStatus(orderId, status);
            if (success) {
                response.sendRedirect(request.getContextPath() + "/admin/orders?success=订单状态更新成功");
            } else {
                response.sendRedirect(request.getContextPath() + "/admin/orders?error=订单状态更新失败");
            }
        } catch (NumberFormatException e) {
            response.sendRedirect(request.getContextPath() + "/admin/orders?error=无效的订单ID");
        }
    }
}